package com.data_structure_algorithm.data_structure.linked;

import java.util.Objects;

/**
 * @author 何章怀晓 hezhanghuaixiao
 * @version V1.0
 * @desc
 */
public class TwoLinked {

  //创建双链表head
  Node head = new Node(0, "");

  /**
   * 添加节点到双向链表的末尾
   *
   * @param node {@link Node} 新增节点
   */
  public void add(Node node) {
    Node temp = this.head;
    while (null != temp.next) {
      temp = temp.next;
    }
    temp.next = node;
    node.pre = temp;
  }

  public void update(Node head, Node node) {
    if (head == null) {
      return;
    }

    Node temp = head;

    //这里。next是因为有头结点，每次传来的都是head
    boolean flag = false;
    while (true) {
      if (temp.next == null) {
        break;
      }

      if (temp.next.id == node.id) {
        flag = true;
        break;
      }

      temp = temp.next;
    }

    if (flag) {
      //找到了
      temp.next.name = node.name;
    } else {
      System.out.println("无此节点");
    }

    preTraverse(head);
  }

  public void remve(Node node) {
    if (node == null) {
      return;
    }

    Node temp = head.next;
    boolean flag = false;

    while (true) {
      if (temp == null) {
        break;
      }

      if (temp.id == node.id) {
        flag = true;
        break;
      }

      temp = temp.next;
    }

    if (flag) {
      Node cur = temp;

      //判断要删除节点是否为最后一个节点
      if (cur.next == null) {
        //

        cur.pre.next = null;
        return;
      }
      //删除当前节点
      //将当前节点前节点的后节点置为当前节点的后节点

      cur.pre.next = cur.next;
      //将当前节点后节点的前节点置为当前节点的前节点
      cur.next.pre = cur.pre;
    } else {
      System.out.println("无此数据");
    }
  }


  public void preTraverse(Node node) {
    if (node == null) {
      return;
    }
    Node temp = node;

    while (temp != null) {
      System.out
          .println("Node:     pre：" + temp.pre + "     next：" + temp.next + "     id：" + temp.id
              + "         name: " + temp.name);
      temp = temp.next;
    }

  }

  public void postTraverse(Node node) {
    if (node == null) {
      return;
    }

    Node temp = node;
    //先找到最后节点
    while (true) {
      if (temp == null) {
        break;
      }
      temp = temp.next;
    }

    while (temp.pre != null) {
      System.out
          .println("Node:     pre：" + temp.pre + "     next：" + temp.next + "     id：" + temp.id
              + "         name: " + temp.name);

      temp = temp.pre;
    }
  }


  public void sortAdd(Node node) {
    if (Objects.isNull(node)) {
      return;
    }

    //按照编号添加
    Node temp = head; //首节点

    boolean flag = false;
    while (true) {
      if (temp.next == null) {
        break;
      }

      if (temp.next.id > node.id) {
        flag = true;
        break;

      }

      temp = temp.next;
    }

    Node cur = temp.next == null ? temp : temp.next;
    if (flag) {
      //找到合适的位置
      //获得当前节点

      //新节点需要添加到当前节点的前面
      //将当前节点的前节点的后节点设置为新节点
      cur.pre.next = node;
      //新节点的pre为当前节点的前节点
      node.pre = cur.pre;
      //将当前节点的前节点设置为新节点
      cur.pre = node;
      //将新节点的next节点设置为当前节点
      node.next = cur;


    } else {
      //直接添加
      cur.next = node;
      node.pre = cur;
    }


  }


  public static void main(String[] args) {
    TwoLinked twoLinked = new TwoLinked();
    twoLinked.sortAdd(new Node(1, ""));
    twoLinked.sortAdd(new Node(2, ""));
    twoLinked.sortAdd(new Node(7, ""));

    twoLinked.sortAdd(new Node(5, ""));

    twoLinked.preTraverse(twoLinked.head);
  }


}


class Node {

  Node pre;

  Node next;

  int id;

  String name;


  public Node(int id, String name) {
    this.id = id;
    this.name = name;
  }


  @Override
  public String toString() {
    return "Node{" + id +
        '}';
  }
}
